package com.haochang.chunkvod.controller.adapter.hostess;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.controller.activity.hostess.SortRoomSecondActivity;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.view.vod.listView.DragSwapListView;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * @author Yangjie
 *         className StringAdapter
 *         created at  2016/8/24  19:16
 *         开房排序 分区排序
 */
public class SortRoomAreaAdapter extends BaseDragSwapAdapter<SortRoomAreaAdapter.ViewHolder, HostessInfo> implements DragSwapListView.OnDragACTION_UP
{
	private int keyOfTag = R.string.app_name;
	private Context context;
	private DragSwapListView listView;
	private List<HostessInfo> list;
	public List<HostessInfo> listTemp;
	private JSONArray array;
	public JSONArray arrayAfter;//变化后的array
	private long lastActionUpMilles;//最后手指离开时间，防止按的太快
	private boolean recordTimeMilles = false;//是否更新最后手指拿起的时间

	public SortRoomAreaAdapter(Context context, List<HostessInfo> list, DragSwapListView listView, JSONArray array, JSONArray arrayAfter)
	{
		super(list);
		this.context = context;
		this.listView = listView;
		this.list = list;
		listTemp = new ArrayList<HostessInfo>();
		listTemp.addAll(list);
		listView.setCallBack(this);
		this.array = array;
		this.arrayAfter = arrayAfter;
		Logger.e("第二个adapter构造 ：" + array);
	}

	@Override
	public ViewHolder createViewHolder(LayoutInflater inflater, int position)
	{
		final ViewGroup nullParent = null;
		View view = inflater.inflate(R.layout.item_sort_room_area,nullParent, false);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder(final Context context, ViewHolder holder, final int position)
	{
		final HostessInfo info = getItem(position);
		holder.tv_room.setText(info.getContent());
		holder.iv_sort_right.setVisibility(info.isShowRightDrawable() ? View.VISIBLE : View.INVISIBLE);
		holder.iv_arrow_right.setVisibility(info.isShowRightDrawable() ? View.INVISIBLE : View.VISIBLE);
		if (listView.isCanDrag())
		{
			holder.rele_sort_top.clearFocus();
			holder.rele_sort_top.setClickable(false);
			holder.rele_sort_top.setEnabled(true);
			holder.rele_sort_top.setTag(keyOfTag, position);
			holder.rele_sort_top.setOnTouchListener(onTouchListener);
		} else
		{
			holder.rele_sort_top.setClickable(true);
			holder.rele_sort_top.setEnabled(true);
			holder.rele_sort_top.setTag(info);
			holder.rele_sort_top.setOnClickListener(clickListener);
		}
	}

	OnBaseClickListener clickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			HostessInfo info = (HostessInfo) v.getTag();
			if (null != info)
			{
				Intent intent = new Intent(context, SortRoomSecondActivity.class);
				intent.putExtra(IntentKey.SORT_ROOM_NAME, info.getContent());
				intent.putExtra(IntentKey.SORT_ROOM_AREA_ID, info.getAreaId());
				intent.putExtra(IntentKey.JSONArray, arrayAfter.length() <= 0 ? array.toString() : arrayAfter.toString());
				Logger.e("传递："+intent.getStringExtra(IntentKey.JSONArray));
				context.startActivity(intent);
			}
		}
	};
	View.OnTouchListener onTouchListener = new View.OnTouchListener()
	{
		@Override
		public boolean onTouch(View v, MotionEvent motionEvent)
		{
			int position = (Integer) v.getTag(keyOfTag);
			if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
			{
				if (System.currentTimeMillis() - lastActionUpMilles >= 500 || lastActionUpMilles == 0)
				{
					recordTimeMilles = true;
					listView.startShadow(position);
				} else
				{
					recordTimeMilles = false;
				}
			}
			return false;
		}
	};

	/**
	 * 判断操作（置顶，拖拽）过程中是否有变化，拖拽之后listTemp有变化，不是元数据，不能作为最后数据变化来判断
	 *
	 */
	public boolean hasDataChanged()
	{
		try
		{
			int len = list.size();
			for (int i = 0; i < len; i++)
			{
				if (!TextUtils.equals(list.get(i).getAreaId(), listTemp.get(i).getAreaId()))
				{
					return true;
				}
			}
		} catch (Exception e)
		{
			Logger.e("咨客按房型排序_"+e.getMessage());
		}
		return false;
	}

	/**
	 * 手指释放时监听
	 */
	@Override
	public void onActionUp()
	{
		if (recordTimeMilles)
		{
			lastActionUpMilles = System.currentTimeMillis();
		}
	}

	class ViewHolder extends BaseDragSwapAdapter.BaseViewHolder
	{
		public BaseTextView tv_room;//文字设置
		public ImageView iv_sort_right;//右边排序icon
		public ImageView iv_arrow_right;//右边箭头icon
		public RelativeLayout rele_sort_top;//布局上半部分

		protected ViewHolder(View mView)
		{
			super(mView);
			tv_room = (BaseTextView) mView.findViewById(R.id.tv_room);
			iv_sort_right = (ImageView) mView.findViewById(R.id.iv_sort_right);
			iv_arrow_right = (ImageView) mView.findViewById(R.id.iv_arrow_right);
			rele_sort_top = (RelativeLayout) mView.findViewById(R.id.rele_sort_top);
		}
	}
}
